<table width="100%"><tr><td width="20%">
<a href="../index.html">&lt; index</a><br />
<a href="index.html">&lt; 14. BSP toolkit</a><br />
<a href="bsp_init.html">&lt; 14.1 Creating a BSP tree</a>
</td><td width="60%">
<p align="center">
=====================================<br />
14.2 Splitting a BSP tree<br />
=====================================<br />
</p></td><td width="20%">
<a href="bsp_resize.html">&gt; 14.3 Resizing a BSP tree</a>
</td></tr></table>
<style type="text/css">
.code { color:#444444; background-color:#EEEEEE;}
pre { color:#444444; background-color:#EEEEEE;}
table.param td { border : 1px solid #000000; }
th { background-color: #44BBFF; color: #FFFFFF }
table.none td { border : 0 }
</style>
Once you have the root node, you can split it into two smaller non-overlapping nodes.<br />
<p class="code"><pre>
C++ : void TCODBsp::splitOnce(bool horizontal, int position)
C   : void TCOD_bsp_split_once(TCOD_bsp_t *node, bool horizontal, int position)
Py  : bsp_split_once(node, horizontal, position)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>node</td><td>In the C version, the root node created with <a href="bsp_init.html">TCOD_bsp_new_with_size</a>, or a node obtained by splitting.</td></tr>
<tr><td>horizontal</td><td>If true, the node will be splitted horizontally, else, vertically.</td></tr>
<tr><td>position</td><td>Coordinate of the splitting position.<br />If horizontal is true, x &lt;= position &lt; x+w<br />Else, y &lt;= position &lt; y+h</td></tr>
</table>
<br />
Example :<br />
<p class="code"><pre>
C++ : TCODBsp *myBSP = new TCODBsp(0,0,50,50);
      myBSP->splitOnce(true,20); // horizontal split into two nodes : (0,0,50,20) and (0,20,50,30)
C   : TCOD_bsp_t *my_bsp=TCOD_bsp_new_with_size(0,0,50,50);
      TCOD_bsp_split_once(my_bsp,false,20); /* vertical split into two nodes : (0,0,20,50) and (20,0,30,50)
Py  : my_bsp=libtcod.bsp_new_with_size(0,0,50,50)
      libtcod.bsp_split_once(my_bsp,False,20) # vertical split into two nodes : (0,0,20,50) and (20,0,30,50)
</pre></p>
<br />You can also recursively split the bsp. At each step, a random orientation (horizontal/vertical) and position are choosen :<br />
<p class="code"><pre>
C++ : void TCODBsp::splitRecursive(TCODRandom *randomizer, int nb, int minHSize, int maxHRatio, int minVSize, int maxVRatio)
C   : void TCOD_bsp_split_recursive(TCOD_bsp_t *node, TCOD_random_t randomizer, int nb, int minHSize, int minVSize, float maxHRatio, float maxVRatio)
Py  : bsp_split_recursive(node, randomizer, nb, minHSize, minVSize, maxHRatio, maxVRatio)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>node</td><td>In the C version, the root node created with <a href="bsp_init.html">TCOD_bsp_new_with_size</a>, or a node obtained by splitting.</td></tr>
<tr><td>randomizer</td><td>The <a href="../random/index.html">random number generator</a> to use. Use NULL for the default one.</td></tr>
<tr><td>nb</td><td>Number of recursion levels.</td></tr>
<tr><td>minHSize, minVSize</td><td>minimum values of w and h for a node. A node is splitted only if the resulting sub-nodes are bigger than minHSize x minVSize</td></tr>
<tr><td>maxHRatio, maxVRation</td><td>maximum values of w/h and h/w for a node. If a node does not conform, the splitting orientation is forced to reduce either the w/h or the h/w ratio. Use values near 1.0 to promote square nodes.</td></tr>
</table>
<br />
Example :<br />
Do a 4 levels BSP tree (the region is splitted into a maximum of 2*2*2*2 sub-regions).<br />
<p class="code"><pre>
C++ : TCODBsp *myBSP = new TCODBsp(0,0,50,50);
      myBSP->splitRecursive(NULL,4,5,5,1.5f,1.5f); 
C   : TCOD_bsp_t *my_bsp=TCOD_bsp_new_with_size(0,0,50,50);
      TCOD_bsp_split_recursive(my_bsp,NULL,4,5,5,1.5f,1.5f);
Py  : my_bsp=libtcod.bsp_new_with_size(0,0,50,50)
      libtcod.bsp_split_recursive(my_bsp,0,4,5,5,1.5,1.5)
</pre></p>

